package String;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Classname JZ58字符串翻转
 * @Description TODO
 * @Date 2023/3/6 23:24
 * @Created by xjl
 */
public class JZ58字符串翻转 {

    /**
     * @description 分割然后翻转
     * @param: s
     * @date: 2023/3/6 23:20
     * @return: java.lang.String
     * @author: xjl
     */
    public String reverseWords(String s) {
        // 除去开头和末尾的空白字符
        s = s.trim();
        // 正则匹配连续的空白字符作为分隔符分割
        List<String> wordList = Arrays.asList(s.split("\\s+"));
        Collections.reverse(wordList);
        return String.join(" ", wordList);
    }
}
